/**
 * @author Phuc "Noel" Vo
 */
package com.example.noelwhereismystuffs.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;

public class Database implements Serializable {
	private static final long serialVersionUID = 2L;
	private static Map<String, User> userDatabase = new HashMap<String, User>();
	private static Map<String, List<Item>> lostItemDatabase = new HashMap<String, List<Item>>();
	private static List<Item> exampleList = new ArrayList<Item>(
			Arrays.asList(new LostItem("mouse", userDatabase
					.get("pvo&@gatech.edu"), "Klause","Electronics","12/25/92")));
	// Initialize some people for the model and put them in the collection
	static {
		userDatabase.put("pvo7@gatech.edu", new User("pvo7@gatech.edu",
				"123123"));
		userDatabase.put("a", new User("a", "a"));
		userDatabase.put("admin", new Admin("Noel", "admin", "admin"));
	}

	static {
		lostItemDatabase.put("gatech", exampleList);
	}

	// Initialize some item for the model and put them in the collection
	static {
		lostItemDatabase.put("pvo7@gatech.edu", exampleList);
	}

	/**
	 * Get the User in the database fro its email
	 * 
	 * @param email
	 * @return User
	 */
	protected User getUser(String email) {
		return userDatabase.get(email);
	}

	/**
	 * Add new User to the database
	 * 
	 * @param username
	 * @param email
	 * @param password
	 */
//	protected void addUser(String username, String email, String password) {
//		userDatabase.put(email, new User(username, email, password));
//	}
	


	/**
	 * Add new Admin to the database
	 * 
	 * @param username
	 * @param email
	 * @param password
	 */
	protected void addAdmin(String username, String email, String password) {
		userDatabase.put(email, new Admin(username, email, password));
		Log.d("Database", "New admin has been added");
	}

	/**
	 * Get Lost Item List of the User
	 * 
	 * @param user
	 * @return List of lost items
	 */
	protected List<Item> getLostList(User user) {
		// List<LostItem> example = new ArrayList<LostItem>(Arrays.asList(
		// new LostItem("mouse", new User("pvo7", "123123"), "Klause"),
		// new LostItem("shit", new User("pvo7", "123123"), "Klause")));
		List<Item> list = lostItemDatabase.get(user.getEmail());
		return list;
	}

	/**
	 * Add new items to database
	 * 
	 * @param owner
	 * @param itemType
	 * @param itemName
	 * @param itemLocation
	 * @param category
	 */
	public ArrayList<Item> addNewItem(User owner, String itemType, String itemName,
			String itemLocation, String category, String itemDate) {
		addNewLostItem(owner, itemName, itemLocation, category, itemDate);
		return (ArrayList<Item>) getLostList(owner);
	}

	/**
	 * Add new Lost items to database
	 * 
	 * @param owner
	 * @param itemName
	 * @param itemLocation
	 */
	private void addNewLostItem(User owner, String itemName, String itemLocation, String itemCategories, String itemDate) {
		List<Item> lostItemList;
		if (lostItemDatabase.get(owner.getEmail()) != null) {
			lostItemList = lostItemDatabase.get(owner.getEmail());
		} else {
			lostItemList = new ArrayList<Item>();
		}

		lostItemList.add(new LostItem(itemName, owner, itemLocation, itemCategories,itemDate ));
		lostItemDatabase.put(owner.getEmail(), lostItemList);
	}
	
	public ArrayList<Item> getItemList(User user){
		return (ArrayList<Item>) getLostList(user);
	}

	/**
	 * Remove user from database
	 * 
	 * @param email
	 */
	public void removeUser(String email) {
		userDatabase.remove(email);
		Log.d("Database", "User has been removed");
	}

	public List<Item> getCategoriesFilteredList(User user, String categories) {
		List<Item> itemList = lostItemDatabase.get(user.getEmail());
		List<Item> categoriesList = new ArrayList<Item>();
		for (Item i : itemList) {
			if (i.getCategory().equals(categories)) {
				categoriesList.add(i);
			}
		}
		return categoriesList;
	}
	
	public List<Item> getStatusFilteredList(User user, String status) {
		List<Item> itemList = lostItemDatabase.get(user.getEmail());
		List<Item> statusFilteredList = new ArrayList<Item>();
		for (Item i : itemList) {
			if (i.getStatus().equals(status)) {
				statusFilteredList.add(i);
			}
		}
		return statusFilteredList;
	}
	
	public List<Item> getDateFilteredList(User user, String date) {
		List<Item> itemList = lostItemDatabase.get(user.getEmail());
		List<Item> dateFilteredList = new ArrayList<Item>();
		for (Item i : itemList) {
			if (i.getDate().equals(date)) {
				dateFilteredList.add(i);
			}
		}
		return dateFilteredList;
	}
}
